package com.wxy.ej.flow.modules.excelOnline.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rt.easyjava.common.base.MyQueryWrapper;
import com.rt.easyjava.common.base.PageDto;
import com.rt.easyjava.common.base.UserDetailsDto;
import com.wxy.ej.flow.modules.excelOnline.bean.ExcelOnline;
import com.wxy.ej.flow.modules.excelOnline.bean.ExcelOnlineUserLink;
import com.wxy.ej.flow.modules.excelOnline.dto.ExcelOnlineDto;
import com.wxy.ej.flow.modules.excelOnline.dto.ExcelOnlineUserLinkDto;
import com.wxy.ej.flow.modules.excelOnline.service.ExcelOnlineBizService;
import com.wxy.ej.flow.modules.excelOnline.service.beanService.ExcelOnlineService;
import com.wxy.ej.flow.modules.excelOnline.service.beanService.ExcelOnlineUserLinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.rt.easyjava.common.base.Result;
import lombok.extern.slf4j.Slf4j;
import com.rt.easyjava.common.config.dozer.MyMapper;

/**
* 业务前端控制器
* 开发规范
* 1，定义具有业务含义的控制方法：可以复制beanController中的相关方法，重命名成具有业务含义，删除beanController中没有使用的方法，精简代码;
* 2，controller中所有方法必须加权限注释；注释类似：@PreAuthorize("hasAuthority('task:query')")；不加权限注释，外网可匿名访问，有安全风险；
* 3，微服务之间访问，方法上可不加@PreAuthorize，但controller的访问路径必须包含"innerService"，网关中拦截了路径中包含innerService的服务请求，访问路径类似：/base/innerService/shortToken/selectBy；
* 4，用户信息可直接形参获取：String (_userId，_loginName，_userName，_companyId，userDetailsJson)，参考相应beanController实现;
* 5，当前是Biz类（自定义业务代码类），重新生成源码时，对应类的覆盖情况如下：
*    会重写对应的bean类：bean、beanController、dto、beanService、mapper、dozer；
*    不会覆盖Biz相关类：如：XxxBizController,XxxBizService,XxxRepository,XxxFacade。
* @author wuxiaoyong
* @since 2022-05-14
*/
@Slf4j
@Api(tags = "ExcelOnlineBizController - 业务控制类")
@RequestMapping("/coding/excelOnlineBiz")
@Controller
public class ExcelOnlineBizController {
    @Autowired
    protected MyMapper mapper;
    @Autowired
    protected ExcelOnlineBizService excelOnlineBizService;
    @Autowired
    protected ExcelOnlineService excelOnlineService;

    @PreAuthorize("hasAuthority('taskflow:excelOnline:list')")
    @ApiOperation("自定义sql分页查询（mapper.xml, 适合多表查询）")
    @GetMapping(value = "/myPage")
    @ResponseBody
    public Result myPage(@ModelAttribute ExcelOnlineDto excelOnlineDto, String userDetailsJson){
        UserDetailsDto userDetailsDto = UserDetailsDto.getUserDetailsDto(userDetailsJson, "excelOnline:query");
        Page<ExcelOnline> page = new Page<>(excelOnlineDto.getPage(), excelOnlineDto.getLimit());

        // 查询条件
        ExcelOnline excelOnlineQuery = mapper.map(excelOnlineDto, ExcelOnline.class);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        excelOnlineQuery.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        excelOnlineQuery.rebuildDataLimitQueryBean(userDetailsDto);


        // 多表查询时，plus自带不支持，这里封装一个工具类处理，mapper.xml中主表别名是t1, 给所有非空字段加别名； 特殊查询条件，在工具类中要忽略掉，最后自定义增加。
        QueryWrapper<ExcelOnline> queryWrapper = MyQueryWrapper.buildQueryWrapper(excelOnlineQuery , "create_time", "name");
        //特殊查询条件
        queryWrapper.ge(excelOnlineDto.getCreateTime_Start()!=null,"t1.create_time",excelOnlineDto.getCreateTime_Start());
        queryWrapper.le(excelOnlineDto.getCreateTime_End()!=null,"t1.create_time",excelOnlineDto.getCreateTime_End());
        // 查登录用户的数据
        queryWrapper.eq("t2.tmpt_auth_user_id",userDetailsDto.getUserId());
        queryWrapper.like(excelOnlineQuery.getName()!=null, "t1.name",excelOnlineQuery.getName());
        // queryWrapper.orderBy(excelOnlineDto.getCondition(), excelOnlineDto.isAsc(), "t1."+excelOnlineDto.getColumn());
        // 支持前端：listQuery.sort='-part_no,-id'
        for (PageDto sortDto:excelOnlineDto.getSortList()){
            queryWrapper.orderBy(true, sortDto.getAsc(), "t1."+sortDto.getColumn());
        }

        return excelOnlineService.myPage(page, queryWrapper, excelOnlineDto.getHasFooter());
    }

    @PreAuthorize("hasAuthority('taskflow:excelOnline:create')")
    @ApiOperation("新增对象")
    @PostMapping(value = "/create")
    @ResponseBody
    public Result create(@RequestBody ExcelOnlineDto excelOnlineDto, String userDetailsJson){
        //插入参数
        excelOnlineDto.setCreatorUpdatorInfo(userDetailsJson);
        //调用服务
        return excelOnlineBizService.createExcel(excelOnlineDto);
    }


    @PreAuthorize("hasAuthority('taskflow:excelOnline:create')")
    @ApiOperation("新增对象")
    @PostMapping(value = "/bindExcelByExcelOnlineId")
    @ResponseBody
    public Result bindExcelByExcelOnlineId(@RequestBody ExcelOnlineDto excelOnlineDto){
        //调用服务
        return excelOnlineBizService.bindExcelByExcelOnlineId(""+excelOnlineDto.getId());
    }

    @PreAuthorize("hasAuthority('taskflow:excelOnline:delete')")
    @PostMapping(value = "/deleteLink")
    @ResponseBody
    public Result deleteLink(String id){
        //调用服务
        return excelOnlineBizService.deleteLink(id);
    }


    @PreAuthorize("hasAuthority('taskflow:excelOnline:create')")
    @ApiOperation("备份数据")
    @PostMapping(value = "/backupData")
    @ResponseBody
    public Result backupData(@RequestBody ExcelOnlineDto excelOnlineDto){

        //调用服务
        return excelOnlineBizService.backupData(excelOnlineDto);
    }

}
